package cn.jnetty.ioc;
import cn.jnetty.api.annotation.JComponentScans;
import cn.jnetty.api.handler.RouterHandler;
import cn.jnetty.ioc.definition.JDefaultBeanDefinitionFactory;
import cn.jnetty.ioc.mapping.JRequestDefinitionBeanFactory;
import cn.jnetty.ioc.scan.JnettyIocResourceScanning;
import java.lang.annotation.Annotation;
import java.util.*;
/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/4/3 17:28
 * 描述：
 */
public class JNettyApplication {
    public static JNettyApplication run(Class<?> primarySources, String... args) {
        return new JNettyApplication(primarySources).run(args);
    }

    private JNettyApplication(Class<?> primarySources) {
        this.primarySources = primarySources;
    }

    private Class<?> primarySources;

    private JNettyApplication run(String[] args) {
        for (Annotation annotation : this.primarySources.getAnnotations()) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if (annotationType == JComponentScans.class) {
                JComponentScans scan = JComponentScans.class.cast(annotation);
                String[] value = scan.value();
                //默认扫描cn.jnetty包和当前包
                int l = value.length;
                String n = primarySources.getName();
                n = n.substring(0, n.lastIndexOf("."));
                /**n的结果 比如传进来的
                 * primarySources.getName() = cn.test.controller.Main
                 * 那么n = cn.test.controller
                 * 而cn.jnetty为jnetty的包，这里默认扫描
                 */
                String[] defaultScan = {n, "cn.jnetty"};
                int len = 2;
                if (n.startsWith("cn.jnetty")) {
                    defaultScan = new String[]{"cn.jnetty"};
                    len = 1;
                }
                //去除重复
                value = Arrays.copyOf(value, value.length + len);
                System.arraycopy(defaultScan, 0, value, l, len);
                value = removeRepetition(value);
                JnettyIocResourceScanning.iocLoader(value);
            }
        }
        return this;
    }
    /**
     * 去除重复
     *
     * @param v
     * @return
     */
    private String[] removeRepetition(String[] v) {
        if (v == null || v.length == 1) {
            return v;
        }
        //去除空格
        for(int i = 0;i<v.length;i++){
            v[i] = v[i].trim();
        }
        //替换包含
        for(int i = 1;i<v.length;i++){
            String str = v[i];
            for(int j = 0;j<v.length;j++){
                String sj = v[j];
                if(str.startsWith(sj)){
                    v[i] = v[j];
                }else {
                    if(sj.startsWith(str)){
                        v[j] = v[i];
                    }
                }
            }
        }
        //去重
        Set<String> set = new HashSet<>(v.length);
        for(int i = 0;i<v.length;i++){
            set.add(v[i]);
        }
        String[] strings = set.toArray(new String[set.size()]);
        return strings;
    }

    public static void main(String[] args) {
//        String[] s = {"cn.gf.nn","cn.jnetty"};
//        System.out.println(Arrays.toString(removeRepetition(s)));
    }
    //[0][0][0][0]

    //[1][1][1][1]
    public static Object getBean(String beanName) {
        return JDefaultBeanDefinitionFactory.getInstance().getBean(beanName);
    }

    /**
     * 建议只有一次注入的时候使用，因为底层使用了遍历容器查找
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return (T) JDefaultBeanDefinitionFactory.getInstance().getBean(clazz);
    }

    public static RouterHandler getDefinitionBean(String path) {
        return JRequestDefinitionBeanFactory.getInstance().getBean(path);
    }
}
